package java8Com.Functional;

import java8Com.chap2.FilteringApples;
import org.omg.CORBA.portable.ValueOutputStream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;


/**
 * 函数式接口
 */
public class function {
    /**
     * 断言接口
     * @param list
     * @param p
     * @param <T>
     * @return
     */
    public static <T> List<T> predicate(List<T> list,Predicate<T> p){
        List<T> resultList = new ArrayList<>();
        for (T t : list) {
            if (p.test(t)){
                resultList.add(t);
            }
        }

        return resultList;
    }

    public static <T> void consumer(List<T> list,Consumer<T> consumer){
        list.forEach(x ->{
            consumer.accept(x);
        });
    }

    public static <T,R> List<R> map(List<T> list, Function<T,R> function){
        List<R> result = new ArrayList<>();
        list.forEach(x ->{
            result.add(function.apply(x));
        });

        return result;
    }

    public static Integer getMax(Supplier<Integer> supplier){
        return supplier.get();
    }

    public static Integer findMax(){
        int[] numbers = {100, 200, 300, 400, 500, -600, -700, -800, -900, -1000};

        Integer max1 = getMax(() -> {
            int max = numbers[0];
            for (int number : numbers) {
                if (number > max) {
                    max = number;
                }
            }
            return max;
        });

        return max1;
    }

    //模拟reduce累加
    public static <T> T match(List<T> list,T t,BinaryOperator<T> binaryOperator){
        T result=t;
        for (T t1 : list) {
            result=binaryOperator.apply(t,t1);
        }

        return result;
    }

    public static int matchInt(int[] list,int init,IntBinaryOperator binaryOperator){
        int result=init;
        for (int i : list) {
            result=binaryOperator.applyAsInt(result,i);
        }

        return result;
    }


    public static void main(String[] args) {


        Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println(match(Arrays.asList(numbers), 0, (x, y) -> x + y));


        int[] numbers1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println(matchInt(numbers1, 0, (x, y) -> x + y));


//        BiFunction<Integer,Integer,Integer> function=(Integer x1,Integer x2) ->x1+x2;
//        System.out.println(function.apply(2,3));
//
//        BinaryOperator<Integer> f=(Integer x1,Integer x2) ->x1+x2;
//        System.out.println(f.apply(1, 2));
//        List<Integer> predicate = predicate(Arrays.asList(1, 2, 4, 5), (Integer i) -> i > 3);
//        predicate.forEach(x -> System.out.println(x));
//
//        consumer(Arrays.asList(1, 2, 4, 5),(Integer i) -> System.out.println(i));

//        List<Integer> map = map(Arrays.asList("java8", "in", "action"), (String s) -> s.length());
//        map.forEach(x -> System.out.println(x));

        //System.out.println(findMax());
    }

}
